package chat.chit.tit.mit.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import chat.chit.tit.mit.R;
import chat.chit.tit.mit.model.PersonEntity;

public class ContactsAdapter extends BaseAdapter {

	private Context mContext;
	private List<PersonEntity> listPerson;
	private LayoutInflater inflater;

	public ContactsAdapter(Context context, List<PersonEntity> listPerson) {
		this.mContext = context;
		this.listPerson = listPerson;
		this.inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return listPerson.size();
	}

	@Override
	public Object getItem(int position) {
		return listPerson.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("ViewHolder")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = inflater
					.inflate(R.layout.item_contact, parent, false);
			viewHolder.iconStatus = (ImageView) convertView
					.findViewById(R.id.img_status);
			viewHolder.tvEmail = (TextView) convertView
					.findViewById(R.id.tv_email);
			viewHolder.tvFullName = (TextView) convertView
					.findViewById(R.id.tv_fullname);
			viewHolder.btnType = (ImageView) convertView
					.findViewById(R.id.btn_type);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (listPerson.get(position).isStatus()) {
			viewHolder.iconStatus.setVisibility(View.VISIBLE);
		} else {
			viewHolder.iconStatus.setVisibility(View.INVISIBLE);
		}

		viewHolder.tvEmail.setText(listPerson.get(position).getEmail());
		viewHolder.tvFullName.setText(listPerson.get(position).getFullName());
		
		switch (listPerson.get(position).getType()) {
		case PersonEntity.CHO_KET_BAN:
			viewHolder.tvEmail.setText("Dang cho ket ban");
			break;
		case PersonEntity.CHUA_KET_BAN:
			viewHolder.tvEmail.setText("Chua ket ban");
			viewHolder.btnType.setVisibility(View.VISIBLE);
			break;
		case PersonEntity.DA_KET_BAN:
			viewHolder.tvEmail.setText("Da ket ban");
			break;

		default:
			viewHolder.tvEmail.setText("");
			viewHolder.btnType.setVisibility(View.INVISIBLE);
			break;
		}

		viewHolder.btnType.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
		return convertView;
	}

	public class ViewHolder {
		ImageView iconStatus;
		TextView tvFullName;
		TextView tvEmail;
		ImageView btnType;
	}

}
